package com.laotou99.test2;

import java.security.InvalidAlgorithmParameterException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Base64;

import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.spec.ECParameterSpec;

import com.laotou99.gjCoin.utils.StringUtil;
 


public class Test5 {

	public static void main(String[] args) throws 
		NoSuchAlgorithmException, NoSuchProviderException, 
		InvalidAlgorithmParameterException 
	{ 

		PrivateKey privateKey;
		PublicKey publicKey;
		
        Security.addProvider(new BouncyCastleProvider());
        
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(
               "ECDSA",
                BouncyCastleProvider
                .PROVIDER_NAME);
//secp256k1
        ECParameterSpec ecSpec = 
        		ECNamedCurveTable.getParameterSpec("secp256k1");
        
        keyPairGenerator.initialize(ecSpec, new SecureRandom());

		KeyPair keyPair = keyPairGenerator.generateKeyPair();

		privateKey = keyPair.getPrivate();
		
		byte[] b1 = privateKey.getEncoded();
		
		publicKey = keyPair.getPublic();
		
		byte[] b2 = publicKey.getEncoded();
		
		String str1 = getStringFromKey(privateKey);
		String str2 = getStringFromKey(publicKey);
		System.out.println("privateKey str1-=>"+str1);
		System.out.println("publicKey str2-=>"+str2);
		
		String str3 = StringUtil.applySha256(str1);
		System.out.println("SHA256 publicKey-=>"+str3);
	}
	
	public static String getStringFromKey(Key key) {
		return Base64.getEncoder().encodeToString(key.getEncoded());
	}
}
